package com.test.diy_annotation_aop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.Map;

@Component
public class FindAnnotationRequest {
    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {//获取带有自定义注解的bean
        Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(RestController.class);
        for (Object value : beanMap.values()) {
            Class<?> clazz = value.getClass();
            clazz = clazz.getSuperclass();
            Method[] declaredMethods = clazz.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                //  寻找带有自定义注解的方法
                if (declaredMethod.isAnnotationPresent(TestAnnotation.class)) {
                    // 如果方法上有自定义注解，则获取这个注解
                    TestAnnotation annotation = declaredMethod.getAnnotation(TestAnnotation.class);
                    System.out.println(annotation.value());
                    System.out.println(annotation.desc());

                }
            }
        }
    }
}